#include "tree.h"

/*
 * 20070910 first written: init a tree.
 * 
 */
Node* tree_initNode(void* data)
{
	Node* node = calloc(1,sizeof(Node));
    node->data = data;
    node->brother = NULL;
    node->child = NULL;
	return node;	
}


/*
 * 20070910 first written:find the location of the data.
 * 
 */
int tree_compare(Node * node, void * data)
{
	return (node->data == data);
}

/*
 * 20070910 first written: find the location of the data.
 * 
 */

Node* tree_find(Node* root,void * data)
{
	if(tree_compare(root,data))
	{
		return root;
	}
	else
	{
		if(root->brother == NULL && root->child == NULL)
		{
			return NULL;
		}
		else if(root -> brother != NULL)
		{
			Node * node = root->brother;
			Node * rst = tree_find(node,data);
			if(rst != NULL)
			{
				return rst;
			}
		}
		else if(root->child != NULL)
		{
			Node* node = root->child;
			Node* rst = tree_find(node,data);
			if(rst != NULL)
			{
				return rst;
			}
			else
			{
				return NULL;
			}
		}
		else
		{
			return NULL;
		}
			
		
	}
	return NULL;
}

void tree_traverse(Node * root)
{
	if(root == NULL)
	{
		return;
	}
	printf("%s\n",(char*)root->data);
	tree_traverse(root->child);
	tree_traverse(root->brother);
}


void get_dir_recursely(char* dir_path,Node *root)
{
	DIR * dir;
	dir = opendir(dir_path);
	struct dirent ptr;
    struct dirent * rel = NULL;
    int i=0;
    
    for(readdir_r(dir,&ptr,&rel);rel!=NULL;readdir_r(dir,&ptr,&rel))
    {
    	if((strcmp(ptr.d_name,".")) == 0 || (strcmp(ptr.d_name,"..")) == 0)
    	{
    				continue;
    	}
    	
    	
    	
    	
    	char* curr_path = calloc(strlen(dir_path)+1+strlen(ptr.d_name)+3,1);
    	sprintf(curr_path,"%s/%s",dir_path,ptr.d_name);
    	Node* node = calloc(1,sizeof(Node));
    	node->data = curr_path;    	
        
    	//printf("node->data:%s\n",curr_path);
    	
    	
    	if(i == 0)
    	{
    		//printf("child:\n");
    		root->child = node;    	            
    	}
    	else
    	{
    		//printf("brother:\n");
    	    root->brother = node;
    	}
    	root = node;
    	
    	
    	if(ptr.d_type ==4)
    	{
    	   	get_dir_recursely(curr_path,root);
    	}
    	else
    	{
    		
    	}
    	
    	i++;
    	
    }
}


int tree_case1()
{
	char* src = calloc(256,1);
	strcpy(src,"hello world");
	Node* tree = tree_initNode(src);
	char* t = calloc(256,1);
	strcpy(t,"hello world");
	int rst = tree_compare(tree,src);
	printf("source result:%d\n",rst);
	char* msg = strdup(src);
	int cmp = tree_compare(tree,msg);
	printf("compare result:%d\n",cmp);
	return 0;
}

int tree_case2()
{
    char* root_dir = calloc(1,256);
    strcpy(root_dir,"/bluetooth");
	
	Node * node = calloc(1,sizeof(Node));
    node->data = root_dir;
    
	get_dir_recursely("/bluetooth",node);
	
	tree_traverse(node);
	char* s = calloc(256,1);
	strcpy(s,"hello world\n");
	char* p = s;
	printf(p);
	return 0;
}





void tree_test()
{
	int i = 0;
	i += tree_case1();
	i += tree_case2();
	if( i == 0)
	{
		printf("tree test ok\n");
	}
	else
	{
		printf("tree test failure\n");
	}
}

int ds_test()
{
	tree_test();
	return 0;
}



